package com.tianji.tjpromotion.controller;


import com.tianji.common.domain.dto.PageDTO;
import com.tianji.tjpromotion.domain.dto.CouponFormDTO;
import com.tianji.tjpromotion.domain.dto.CouponIssueFormDTO;
import com.tianji.tjpromotion.domain.vo.CouponDetailVO;
import com.tianji.tjpromotion.domain.vo.CouponPageVO;
import com.tianji.tjpromotion.domain.vo.CouponVO;
import com.tianji.tjpromotion.query.CouponQuery;
import com.tianji.tjpromotion.service.ICouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 优惠券的规则信息 前端控制器
 * </p>
 *
 * @author yp
 * @since 2024-08-02
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/coupons")
@Api(tags = "优惠券相关接口")
public class CouponController {

    private final ICouponService couponService;

    @ApiOperation("新增优惠券接口")
    @PostMapping
    public void saveCoupon(@RequestBody @Valid CouponFormDTO dto) {
        couponService.saveCoupon(dto);
    }


    @ApiOperation("分页查询优惠卷")
    @GetMapping("/page")
    public PageDTO<CouponPageVO> queryPage(CouponQuery query) {
        return couponService.queryPage(query);
    }


    @PutMapping("/{id}/issue")
    @ApiOperation("发布优惠卷")
    public void updateCoupon(@RequestBody CouponIssueFormDTO dto, @PathVariable("id") Long id) {
        dto.setId(id);
        couponService.issueCoupon(dto);
    }


    @PutMapping("/{id}")
    @ApiOperation("修改优惠卷")
    public void updateCoupon(@RequestBody @Valid CouponFormDTO dto, @PathVariable("id") Long id) {
        couponService.updateCoupon(dto, id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除优惠卷")
    public void deleteByCoupon(@PathVariable("id") Long id) {
        couponService.deleteCouponbyId(id);
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id进查询优惠卷")
    public CouponDetailVO queryCouponByid(@PathVariable("id") Long id) {
        return couponService.queryById(id);
    }


    @GetMapping("/list")
    @ApiOperation("用户端查询正在发放的优惠卷")
    public List<CouponVO> queryIssueCouponList() {
        return couponService.queryIssueCouponList();
    }


}
